home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / Misc / NGService / Source / MMText.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  7.5 KB  |  340 lines

  1. /*
  2.  *      You may freely copy, distribute and reuse the code
  3.  *      in this example.  Scott Anguish disclaims any warranty of
  4.  *      any kind, expressed or implied, as to its fitness
  5.  *      for any particular use.
  6.  *        Please keep this notice intact
  7.  *      Written by: Scott Anguish  sanguish@digifix.com
  8.  */
  9.  
  10. #import "MMText.h"
  11.  
  12. #import <libc.h>                      /* MAXPATHLEN */
  13. #import <stdio.h>
  14. #import <strings.h>
  15.  
  16. #import <appkit/appkit.h>
  17.  
  18. #import <streams/streams.h>
  19.  
  20.  
  21. @implementation MMText:Text
  22.  
  23. + initialize
  24. {
  25.     [super initialize];
  26.     [Text registerDirective:"NGFolder" forClass: [MMFolderCell class]];
  27.     [Text registerDirective:"NGGraphic" forClass: [MMGraphicCell class]];
  28.     [Text registerDirective:"NGDocument" forClass: [MMDocumentCell class]];
  29.     return self;
  30. }
  31.  
  32. - (BOOL)isHeaderVisible:(const char*)header
  33. {
  34.     if ((strncmp(header,"From:",5)==0)             && showFrom)  return YES;
  35.     if ((strncmp(header,"Date:",5)==0)             && showDate)  return YES;
  36.     if ((strncmp(header,"Newsgroups:",11)==0)    && showNewsgroups)  return YES;
  37.     if ((strncmp(header,"Subject:",8)==0)        && showSubject)  return YES;
  38.     if ((strncmp(header,"MessageID:",10)==0)    && showMessageID)  return YES;
  39.     if ((strncmp(header,"Path:",5)==0)            && showPath)  return YES;
  40.     if ((strncmp(header,"Followup-To:",12)==0)    && showFollowupTo)  return YES;
  41.     if ((strncmp(header,"Expires:",8)==0)        && showExpires)  return YES;
  42.     if ((strncmp(header,"Reply-To:",9)==0)        && showReplyTo)  return YES;
  43.     if ((strncmp(header,"Sender:",7)==0)        && showSender)  return YES;
  44.     if ((strncmp(header,"Control:",8)==0)        && showControl)  return YES;
  45.     if ((strncmp(header,"Keywords:",9)==0)        && showKeywords)  return YES;
  46.     if ((strncmp(header,"Summary:",8)==0)        && showSummary)  return YES;
  47.     if ((strncmp(header,"Approved:",9)==0)        && showApproved)  return YES;
  48.     if ((strncmp(header,"Lines:",6)==0)            && showLines)  return YES;
  49.     if ((strncmp(header,"Xref:",5)==0)            && showXref)  return YES;
  50.     if ((strncmp(header,"Organization:",13)==0)    && showOrganization)  return YES;
  51.  
  52.     return NO;
  53. }
  54.  
  55. - setVisibleHeaders
  56. {
  57.     showFrom=YES;
  58.     showDate=YES;
  59.     showNewsgroups=NO;
  60.     showSubject=YES;
  61.     showMessageID=NO;
  62.     showPath=NO;
  63.     showFollowupTo=NO;
  64.     showExpires=NO;
  65.     showReplyTo=NO;
  66.     showSender=NO;
  67.     showReferences=NO;
  68.     showControl=NO;
  69.     showDistribution=NO;
  70.     showKeywords=NO;
  71.     showSummary=NO;
  72.     showApproved=NO;
  73.     showLines=NO;
  74.     showXref=NO;
  75.     showOrganization=NO;
  76.  
  77.  
  78.     return self;
  79.  
  80. };
  81. - initFrame:(const NXRect *)frameRect
  82. {
  83.     self = [super initFrame:frameRect];
  84.     [self notifyAncestorWhenFrameChanged:YES];
  85.     [self setMonoFont:NO];
  86.     [self setVertResizable:YES];
  87.     [self setHorizResizable:NO];
  88.     [self setSel:0:0];
  89.     [self setOpaque:YES];
  90.  
  91.     { NXSize aSize = {1.0E38,1.0E38};
  92.       [self setMinSize:&frameRect->size];
  93.       [self setMaxSize:&aSize];
  94.   }
  95.     [self setVisibleHeaders];
  96.     return self;
  97. }
  98.  
  99. - free
  100. {    
  101.  
  102.     [super free];
  103.     return self; 
  104. }
  105.  
  106. - (int)iconEntered:(int)windowNum at:(double)x :(double)y
  107.     iconWindow:(int)iconWindowNum iconX:(double)iconX iconY:(double)iconY
  108.     iconWidth:(double)iconWidth iconHeight:(double)iconHeight
  109.     pathList:(char *)pathList
  110. {
  111.  
  112.  
  113.     if (!iconPathList || strcmp(iconPathList, pathList)) 
  114.         {
  115.         if (iconPathList) NX_FREE(iconPathList);
  116.         NX_MALLOC(iconPathList,char, strlen(pathList)+1);
  117.         //iconPathList = NXCopyStringBufferFromZone(pathList, [self zone]);
  118.         strcpy(iconPathList, pathList);
  119.         }
  120.     return 0;
  121. }
  122.  
  123. - (int)iconReleasedAt:(double)x :(double)y ok:(int *)flag
  124. {
  125.     if (![self isEditable]) 
  126.         {
  127.         *flag = NO;
  128.         } else 
  129.         {
  130.         *flag = YES;
  131.         [self perform:@selector(delayIconReleasedAt:) with:self afterDelay:5 cancelPrevious:YES];
  132.         }
  133.     return 0;
  134. }
  135.  
  136. - delayIconReleasedAt:sender
  137. {
  138.     int dummy,*flag;
  139.     
  140.     NXPoint p;
  141.     char *file,  *tab, *extension;
  142.     id    newCell;
  143.     int foundOne ;
  144.  
  145.     flag = &dummy;
  146.     
  147.     foundOne = NO;
  148.  
  149.     [window convertScreenToBase:&p];
  150.     [self convertPoint:&p fromView:nil];
  151.  
  152.  
  153.     
  154.     file = iconPathList;
  155.     while (file) {
  156.         tab = strchr(file, '\t');
  157.         if (tab) *tab = '\0';
  158.         extension = strrchr(file, '.');
  159.         if (extension &&
  160.         ( !strcmp(extension, ".ps")  || !strcmp(extension, ".eps") ||
  161.           !strcmp(extension,".tiff") || !strcmp(extension,".tif")  )) {
  162.             
  163.             newCell =[[MMGraphicCell alloc] imageFromFilename:file];
  164.             [self replaceSelWithCell:newCell];
  165.             foundOne = YES;
  166.             
  167.         } else {
  168.             newCell = [[MMDocumentCell alloc] imageFromFilename:file];
  169.             [self replaceSelWithCell:newCell];
  170.             foundOne = YES;
  171.         }
  172.             
  173.        file = tab ? ++tab : NULL;
  174.     }
  175.     
  176.     *flag = foundOne;
  177.     return 0;
  178. }
  179.  
  180.  
  181. - becomeFirstResponder
  182. {
  183.     [super becomeFirstResponder];
  184.     return self;
  185. }
  186.  
  187. - appendBoldWord:(char *)aString
  188. {
  189.     int length;
  190.     char header[BUFSIZ],restofline[BUFSIZ];
  191.  
  192.     sscanf(aString,"%s%[^\n]", header,restofline);
  193.     strcat(restofline,"\n");
  194.     
  195.     if ([self isHeaderVisible:header])
  196.         {
  197.         length = [self textLength];
  198.         [self setSel:length:length];
  199.         [self replaceSel:header];
  200.         [self setSel:(length):[self textLength]];
  201.         [self setSelFont:[Font newFont:"Courier-Bold" size:12.0]];
  202.     
  203.         length = [self textLength];
  204.         [self setSel:length:length];
  205.         [self replaceSel:restofline];
  206.         [self setSel:(length):[self textLength]];
  207.         [self setSelFont:[Font newFont:"Courier" size:12.0]];
  208.     
  209.         length = [self textLength];
  210.         [self setSel:length:length];
  211.         };
  212.     return self;
  213.  
  214. };
  215.  
  216. - appendRichFile:(char *)aFile
  217. {
  218.     NXStream *aStream;
  219.     int length;
  220.  
  221.     length = [self textLength];
  222.     [self setSel:length:length];
  223.     aStream=NXMapFile(aFile,NX_READONLY);
  224.     [self replaceSelWithRichText:aStream];
  225.     NXClose(aStream);
  226.     return self;
  227. };
  228.  
  229. - openFileName:(char *)aFile
  230. {
  231.     [self openFileName:aFile ignoreHeader:NO];
  232.     return self;
  233. }
  234.  
  235. - openFileName:(char *)aFile ignoreHeader:(BOOL)ignoreHeader
  236. {
  237.  
  238.     FILE    *filePtr,*outfile;
  239.     char     theLine[BUFSIZ];
  240.     BOOL     NGfound;
  241.     
  242.     [self setText:""];
  243.     NGfound=NO;
  244.     
  245.     [[self window] disableFlushWindow];
  246.     [self setEditable:NO];
  247.     filePtr = fopen(aFile, "r");
  248.     if (ignoreHeader)
  249.         {
  250.         while (fgets(theLine,BUFSIZ, filePtr) != NULL)
  251.             {
  252.                 if (strncmp(theLine, "\n",1)==0)
  253.                     {
  254.                         break;
  255.                     };
  256.                 [self appendBoldWord:theLine];
  257.             };
  258.  
  259.         [self replaceSel:"\n\n"];
  260.         }
  261.     while (fgets(theLine,BUFSIZ, filePtr) != NULL)
  262.         {
  263.             
  264.             if (strncmp(theLine, NGHEADER,strlen(NGHEADER))==0)
  265.                 {
  266.                     NGfound=YES;
  267.                     break;
  268.                 };
  269.         };
  270.     if (NGfound)
  271.         {
  272.  
  273.         if (outfile = popen("rm /tmp/NGtemp; /usr/bin/uudecode ","w"))
  274.             {
  275.             fprintf(outfile,"begin 644 /tmp/NGtemp\n");
  276.             while (fgets(theLine,BUFSIZ, filePtr) != NULL) 
  277.                 {if (strncmp(theLine,"`",1)==0) 
  278.                     {
  279.                         fprintf(outfile,"`\n");
  280.                         break;
  281.                     }
  282.                     else
  283.                     fputs(theLine,outfile);
  284.                 };
  285.             fprintf(outfile,"end\n");
  286.             if (pclose(outfile)==0) [self appendRichFile:"/tmp/NGtemp"];
  287.                 else NXRunAlertPanel("NGService","Unable to decode! Check selection.",
  288.            NULL, NULL, NULL);
  289.             (void)remove("/tmp/NGtemp");
  290.             };
  291.  
  292.         };
  293.     if (!NGfound) NXRunAlertPanel("NGService","I could not find any NewsGrazer Data!",
  294.            NULL, NULL, NULL);
  295.     fclose(filePtr);
  296.     [self sizeToFit];
  297.     [[[self window] reenableFlushWindow] flushWindow];
  298.     [self display];
  299.     return self;
  300.  
  301. }
  302.  
  303. - readFromMemory:(char *)data length:(int)theSize
  304. {
  305.  
  306.     NXStream *aStream;
  307.     char filename[MAXPATHLEN+1];
  308.     
  309.     [self setText:""];
  310.     
  311.     strcpy(filename,"/tmp/file000000Selection" );
  312.     NXGetTempFilename(filename,9);
  313.     aStream = NXOpenMemory(data, theSize ,NX_READONLY);
  314.     NXSaveToFile(aStream, filename);
  315.     NXCloseMemory(aStream,NX_FREEBUFFER);
  316.     [self openFileName:filename];
  317.     (void)remove(filename);
  318.     return self;
  319.  
  320. }
  321.  
  322.  
  323. - saveFileName:(char *)aFile
  324. {
  325.     NXStream *aStream;
  326.  
  327.     aStream = NXOpenMemory(NULL,0, NX_WRITEONLY);
  328.     
  329.     [self writeRichText:aStream];
  330.     
  331.     NXSaveToFile(aStream, aFile);
  332.     NXCloseMemory(aStream, NX_FREEBUFFER);
  333.     
  334.     return self;
  335. }
  336.  
  337.  
  338.  
  339. @end
  340.